package com.hyq.learn.common.util;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.net.InetAddress;
import java.net.UnknownHostException;

/**
 * 主机名工具
 */
public class HostNameUtil {

    private static final Logger logger = LoggerFactory.getLogger(HostNameUtil.class);

    private static final String LOCAL_HOST_NAME;

    static {
        try {
            LOCAL_HOST_NAME = InetAddress.getLocalHost().getHostName();
            logger.info("resolve local host name {}", LOCAL_HOST_NAME);
        } catch (Exception e) {
            logger.error("Error while parsing localHostName.", e);
            throw new RuntimeException(e);
        }
    }

    public static String localHostName() {
        return LOCAL_HOST_NAME;
    }

    public static String localIpAddress() {
        try {
            return InetAddress.getLocalHost().getHostAddress();
        } catch (UnknownHostException e) {
            throw new RuntimeException(e);
        }
    }

}
